package com.gdkf.dormitory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.Admin;
import com.gdkf.dormitory.entity.Dorm;
import com.gdkf.dormitory.entity.Excel;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.service.AdminService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/admin/getAllAdmin")
    public Msg getAllAdmin(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Admin> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        adminService.page(page, null);
        List<Admin> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("adminList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/admin/getOneAdminById")
    public Msg getOneAdminById(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToAdmin(myPage);
            System.out.println(myPage2);
            Page<Admin> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("a_id",((Admin)myPage2.getObject()).getAId());
            Page<Admin> page1 = adminService.page(page, wrapper);
            List<Admin> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("adminList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/admin/getSomeAdminByName")
    public Msg getSomeAdminByName(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToAdmin(myPage);
            System.out.println(myPage2);
            Page<Admin> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("username",((Admin)myPage2.getObject()).getUsername());
            Page<Admin> page1 = adminService.page(page, wrapper);
            List<Admin> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("adminList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/admin/deleteAdminById")
    public Msg deleteAdminById(@RequestBody Admin admin){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("a_id",admin.getAId());
        boolean remove = adminService.remove(wrapper);
        return remove?Msg.success():Msg.fail();
    }

    @PostMapping("/admin/updateAdmin")
    public Msg updateAdmin(@RequestBody Admin admin){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("a_id",admin.getAId());
        return adminService.update(admin,wrapper)?Msg.success():Msg.fail();
    }

    @PostMapping("/admin/addAdmin")
    public Msg addAdmin(@RequestBody Admin admin){
        return adminService.save(admin)?Msg.success():Msg.fail();
    }

    @PostMapping ("/admin/getDetailInformation")
    public Msg getDetailInformation(@RequestBody Admin admin){
//        System.out.println(admin);
        HashMap<String, Object> map = new HashMap<>();
        map.put("aId",admin.getAId());
        map.put("rId",admin.getRId());
        Admin admin2 = adminService.selectAdminRole(map);
        ArrayList<Admin> admins = new ArrayList<>();
        admins.add(admin2);
        return admin!=null?Msg.success().add("adminList",admins):Msg.fail();
    }

    @PostMapping ("/admin/verifyUser")
    public Msg verifyUser(@RequestBody Admin admin){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",admin.getUsername());
        wrapper.eq("password",admin.getPassword());
        wrapper.eq("r_id",admin.getRId());
        Admin one = adminService.getOne(wrapper);
        return one==null?Msg.fail().add("systemMessage","登录失败"):Msg.success().add("admin",one).add("systemMessage","登录成功");
    }


}

